package com.wang.hellohibernate.models;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@SuppressWarnings("restriction")
@Entity
@Table(name = "roles")
@XmlRootElement(name="role")
public class Role implements Serializable
{
	private static final long serialVersionUID = 1532016765322253106L;
	
	public static final String ROLE_USER = "USER";
	public static final String ROLE_MANAGER = "MANAGER";
	public static final String ROLE_ROOT = "ROOT";
	
	@Id
	@GeneratedValue
	@Column(name = "id", unique=true)
	Integer id;

	@Column(name = "roleName")
	private String roleName;
	
	@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
	@Fetch(value = FetchMode.SUBSELECT)
	@JoinTable(name = "roles_permissions",
			joinColumns = {@JoinColumn(name = "roleid", nullable = false, updatable = false) }, 
			inverseJoinColumns = { @JoinColumn(name = "permissionid", nullable = false, updatable = false) })
	private List<Permission> permissions;
	
	@ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
	private List<Person> persons;
	
	@XmlAttribute
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	@XmlElement(nillable=true)
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	
	@XmlElement(nillable=true, name="permissions")
	public List<Permission> getPermissions() {
		return permissions;
	}
	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}

	@XmlElement(nillable=true, name="persons")
	public List<Person> getPersons() {
		return persons;
	}
	public void setPersons(List<Person> persons) {
		this.persons = persons;
	}

	@Override
	public boolean equals(Object obj)
	{
		Role r = (Role)obj;
		if(r.id == id &&r.roleName == roleName)
		{
			return true;
		}
		
		return false;
	}
	
	@Override
	public String toString()
	{
		StringBuffer str = new StringBuffer();
		str.append("[ID: " + id 
				+ " ROLE_NAME: " + roleName
				+ " PERMISSION: {");
		
		if(null != permissions && !permissions.isEmpty())
		{
			Iterator<Permission> permissionIterator = permissions.iterator();
			while(permissionIterator.hasNext())
			{
				Permission permission = permissionIterator.next();
				str.append("[" + permission.toString() + "]");
			}
		}
		
		str.append("}]");
		
		return str.toString();
	}
}
